package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class DelMRitem
 */ 
public class QuerySubSys extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QuerySubSys() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		Widgets wid = new Widgets();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String strSQL = "SELECT COUNT(*) FROM subsys";
		ResultTableSet resultTableSet = new ResultTableSet();
		
		String dataType = request.getParameter("getDataType");
		ArrayList<InfoStruc> infoStrucs = new ArrayList<InfoStruc>();
		
		String bss="";
		
		int page = request.getParameter("page")!=null?Integer.parseInt(request.getParameter("page")):1;
		int total_pages = 1;
		int count  = 0;
		int limit = request.getParameter("page")!=null?Integer.parseInt(request.getParameter("rows")):10;
		
		
		try {
			conn = wid.getConn(true);	
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			rs.next();
			count  = rs.getInt(1);
			
			if ( count  != 0 ) {
				if (limit  < count) {
					total_pages = (count / limit)+1;	
				}
				if ("JSON".equalsIgnoreCase(dataType)) {
					strSQL = "SELECT subsysid,subsysname FROM subsys ORDER BY subsysid ASC";
					ps = conn.prepareStatement(strSQL);
				} else {
					bss ="MBMS";
					strSQL = "SELECT subsysid,subsysname FROM subsys WHERE bssid=?  ORDER BY subsysid ASC";
					ps = conn.prepareStatement(strSQL);
					ps.setString(1, bss);
				}
				
				rs = ps.executeQuery();
				ArrayList<Rows> rows = new ArrayList<Rows>();
				
				
				while ( rs.next() ) {
					String subSysId = rs.getString("subsysid");
					String subSysName = rs.getString("subsysname");
					
					if ("JSON".equalsIgnoreCase(dataType)) {
						InfoStruc infoStruc = new InfoStruc();
						infoStruc.setId(subSysId);
						infoStruc.setName(subSysName);
						infoStrucs.add(infoStruc);
					} else {
						Rows row = new Rows();
						row.setId(subSysId);
						String[] cell = new String[2];
						cell[0] = subSysId;
						cell[1] = subSysName;
						row.setCell(cell);
						rows.add(row);
					}
				}
				resultTableSet.setRows(rows);
				
			} else {
				page =0;
				total_pages=0;
			}
			
			resultTableSet.setPage(String.valueOf(page));
			resultTableSet.setTotal(total_pages);
			resultTableSet.setRecords(String.valueOf(count));
			
		} catch ( SQLException e) {
			System.out.println(wid.errProcess(e));
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}
		}
		
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		if ("JSON".equalsIgnoreCase(dataType)) {
			response.getWriter().write(new Gson().toJson(infoStrucs));
		} else{
			response.getWriter().write(new Gson().toJson(resultTableSet));	
		}
		
				
	}
}